package client.models;

import client.tools.ImagesLoader;
import java.util.ArrayList;
import java.util.HashMap;
import org.newdawn.slick.Image;

/*
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 * ActinoPlayer.java
 *
 * Steven Louie Apr. 8, 2012
 */
public class ActinoPlayer {

    public static final int WALK_SPEED = 1;
    public Direction dir;
    public int speed;
    // Pixel tile positions
    public int px;
    public int py;
    // Absolute tile positions
    public int x;
    public int y;
    // Previous absolute tile positions
    public int prev_x;
    public int prev_y;
    public String name;
    public HashMap<String, ArrayList<Image>> frames = new HashMap<>();
    public String imgName;
    public Image currentImage;
    public int currentImageIndex;
    public boolean animating;

    public ActinoPlayer(String name, String imgName, int x, int y) {
        this.name = name;
        this.imgName = imgName;
        this.x = x;
        this.y = y;
        this.prev_x = x;
        this.prev_y = y;
        speed = WALK_SPEED;
        animating = false;
        dir = Direction.DOWN;
        frames.put("up", ImagesLoader.getInstance().getImages(imgName + "up"));
        frames.put("down", ImagesLoader.getInstance().getImages(imgName + "down"));
        frames.put("right", ImagesLoader.getInstance().getImages(imgName + "right"));
        frames.put("left", ImagesLoader.getInstance().getImages(imgName + "left"));
        currentImageIndex = 0;
        currentImage = frames.get("down").get(currentImageIndex);
    }

    public void initPCor(int tileWidth) {
        px = x * tileWidth;
        py = y * tileWidth;
    }

    public boolean moveUp() {
        if (dir == Direction.UP || speed > 1) {
            dir = Direction.UP;
            prev_y = y;
            y--;
            animating = true;
            return true;
        } else {
            currentImageIndex = 0;
            currentImage = frames.get("up").get(currentImageIndex);
            dir = Direction.UP;
            return false;
        }
    }

    public boolean moveDown() {
        if (dir == Direction.DOWN || speed > 1) {
            dir = Direction.DOWN;
            prev_y = y;
            y++;
            animating = true;
            return true;
        } else {
            currentImageIndex = 0;
            currentImage = frames.get("down").get(currentImageIndex);
            dir = Direction.DOWN;
            return false;
        }
    }

    public boolean moveLeft() {
        if (dir == Direction.LEFT || speed > 1) {
            dir = Direction.LEFT;
            prev_x = x;
            x--;
            animating = true;
            return true;
        } else {
            currentImageIndex = 0;
            currentImage = frames.get("left").get(currentImageIndex);
            dir = Direction.LEFT;
            return false;
        }
    }

    public boolean moveRight() {
        if (dir == Direction.RIGHT || speed > 1) {
            dir = Direction.RIGHT;
            prev_x = x;
            x++;
            animating = true;
            return true;
        } else {
            currentImageIndex = 0;
            currentImage = frames.get("right").get(currentImageIndex);
            dir = Direction.RIGHT;
            return false;
        }
    }
}
